<template>
  <div class="create-post-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" label-position="right" label-width="100px" class="invite-form">
      <el-row :gutter="20">
        <el-col :span="10" :offset="6">
          <el-form-item label="VeevaCode" prop="veevacode">
            <el-input v-model="postForm.veevacode" @change="resetInfo" />
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-button
            plain
            class="btn-block"
            @click="searchVeeva"
          >查 询</el-button>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="postForm.name" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="手机号码" prop="tel">
            <el-input v-model="postForm.tel" type="tel" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="8" :offset="6">
          <el-form-item label="类型" prop="type">
            <el-select
              v-model="postForm.type"
              placeholder="请选择"
              style="width:100%"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="邀请码" prop="invitecode">
            <el-input v-model="postForm.invitecode" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="医院" prop="hospitalName">
            <el-input v-model="postForm.hospitalName" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="科室" prop="departmentid">
            <el-select
              v-model="postForm.departmentid"
              filterable
              clearable
              disabled
              placeholder="请选择"
              style="width:100%"
            >
              <el-option
                v-for="item in deparpmentOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="公司" prop="company">
            <el-input v-model="postForm.company" />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form-item label="部门" prop="department">
            <el-input v-model="postForm.department" />
          </el-form-item>
        </el-col>
      </el-row> -->

      <el-row :gutter="20" class="mt30">
        <el-col :span="12" :offset="6">
          <el-form-item>
            <el-button type="primary" @click="submitForm">提 交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {
  getDepartments
} from '@/api/baseData'
import {
  getUsersList
} from '@/api/users'
import {
  getMeetingInvites,
  addMeetingInvite,
  editMeetingInvite
} from '@/api/meetings'

const defaultForm = {
  id: undefined,
  type: '',
  tel: '',
  veevacode: '',
  invitecode: '',
  name: '',
  hospitalName: '',
  hospid: '',
  departmentid: '',
  company: ''
}

export default {
  name: 'MeetingInviteForm',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      meetingId: '',
      typeOptions: [
        {
          name: '医生',
          id: 1
        },
        {
          name: '员工',
          id: 2
        }
      ],
      deparpmentOptions: [],
      postForm: Object.assign({}, defaultForm),
      loading: false,
      rules: {
        veevacode: [
          { required: true, message: '请输入VeevaCode', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '请输入手机号码', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ]
        // invitecode: [
        //   { required: true, message: '请输入邀请码', trigger: 'blur' }
        // ],
        // hospid: [
        //   { required: true, message: '请选择医院', trigger: 'change' }
        // ],
        // departmentid: [
        //   { required: true, message: '请选择科室', trigger: 'change' }
        // ]
      }
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.meetingId = route.params && route.params.id
      },
      immediate: true
    }
  },
  created() {
    this.fetchDepartments()
    this.meetingId = this.$route.params && this.$route.params.id
    this.postForm.meetingid = this.$route.params && this.$route.params.id
    if (this.isEdit) {
      const inviteId = this.$route.params && this.$route.params.inviteId
      this.fetchData(this.meetingId, inviteId)
    }
  },
  methods: {
    fetchDepartments() {
      getDepartments().then(response => {
        // console.log(response)
        this.deparpmentOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData(id, inviteId) {
      getMeetingInvites({
        meetingid: id,
        inviteid: inviteId
      }).then(response => {
        const { data } = response
        // console.log(data)
        this.postForm.id = data[0].id
        this.postForm.meetingid = data[0].meetingid
        this.postForm.veevacode = data[0].veevacode
        this.postForm.name = data[0].name
        this.postForm.tel = data[0].tel
        this.postForm.type = data[0].type
        this.postForm.invitecode = data[0].invitecode
        this.postForm.hospitalName = data[0].hospitalName
        this.postForm.hospid = data[0].hospid
        this.postForm.departmentid = data[0].departmentid
        this.postForm.company = data[0].company
      }).catch(err => {
        console.log(err)
      })
    },
    searchVeeva() {
      if (this.postForm.veevacode === '') {
        this.$message({
          type: 'error',
          message: '请输入VeevaCode'
        })
      } else {
        getUsersList({
          veevacode: this.postForm.veevacode,
          page: 1,
          size: 10
        }).then(response => {
          const { data } = response
          // console.log(data)
          if (this._.isEmpty(data)) {
            this.$message({
              type: 'error',
              message: response.msg
            })
          } else {
            this.postForm.name = data[0].name
            this.postForm.tel = data[0].tel
            this.postForm.hospitalName = data[0].hospname
            this.postForm.hospid = data[0].hospid
            this.postForm.departmentid = data[0].deparrment
          }
        }).catch(err => {
          console.log(err)
        })
      }
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.isEdit) {
            editMeetingInvite(this.postForm).then(response => {
              // console.log(response)
              this.$notify({
                title: '成功',
                message: '编辑成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.$router.go(-1)
            }).catch(err => {
              console.log(err)
            })
          } else {
            addMeetingInvite(this.postForm).then(response => {
              // console.log(response)
              this.$notify({
                title: '成功',
                message: '添加成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.$router.go(-1)
            }).catch(err => {
              console.log(err)
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetInfo() {
      this.postForm.name = ''
      this.postForm.tel = ''
      this.postForm.hospitalName = ''
      this.postForm.hospid = ''
      this.postForm.departmentid = ''
    },
    resetForm() {
      this.$refs.postForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.create-post-container {
  position: relative;
}
</style>
